const app = getApp();
Page({
  // 页面的初始数据
  data: {
    isStudent: true,
    username: '',
    password: '',
    userType: '',
    a: '',
    b: ''
  },
  // 处理点击“学生”文本切换用户类型
  switchToStudent: function () {
    this.setData({
      isStudent: true
    });
    this.selectComponent('.user-type text[data-type="student"]').setClasses({ selected: true, active: true });
    this.selectComponent('.user-type text[data-type="worker"]').setClasses({ selected: false, active: false });
  },
  // 处理点击“维修师傅”文本切换用户类型
  switchToWorker: function () {
    this.setData({
      isStudent: false
    });
    this.selectComponent('.user-type text[data-type="student"]').setClasses({ selected: false, active: false });
    this.selectComponent('.user-type text[data-type="worker"]').setClasses({ selected: true, active: true });
  },
  inputUsername: function (e) {
    this.setData({
      username: e.detail.value
    });
  },
  inputPassword: function (e) {
    this.setData({
      password: e.detail.value
    });
  },
  login: function () {
    let userType = this.data.isStudent? 'student' : 'worker';
    let url = userType ==='student'? 'http://8.140.225.174:5000/app/login' : 'http://8.140.225.174:5000/worker/login';
    let data = {
      account: this.data.username,
      password: this.data.password
    };
    this.sendLoginRequest(url, data).then((resData) => {
        if (userType ==='student') {
            app.globalData.customer.usermessage = resData;
            console.log(app.globalData.customer.usermessage);
            if (resData.hasOwnProperty('Score')) {
                app.setScore(resData.Score);
            }
            wx.navigateTo({
              url: '/pages/student/student'
            });
          } else {
            app.globalData.worker.workermessage = resData;
            console.log(app.globalData.worker.workermessage);
            wx.navigateTo({
              url: '/pages/worker/worker'
            });
          }
        const token = resData.token; 
        if (token) {
          app.setToken(token); // 存储token
        }
    }).catch((error) => {
      wx.showToast({
        title: '登录失败',
        icon: 'none'
      });
    });
  },
  register: function () {
    let userType = this.data.isStudent? 'student' : 'worker';
    if (userType ==='student') {
      wx.navigateTo({
        url: '/pages/register/student/student'
      });
    } else {
      wx.navigateTo({
        url: '/pages/register/worker/worker'
      });
    }
  },
  sendLoginRequest: function (url, data) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: url,
        method: 'POST',
        data: data,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.code == 200) {
            resolve(res.data.data);
          } else {
            reject(res.data);
          }
        },
        fail: function (err) {
          reject(err);
        }
      });
    });
  },
  sendRegisterRequest: function (url, data) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: url,
        method: 'POST',
        data: data,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.code == 200) {
            resolve(res.data);
          } else {
            reject(res.data);
          }
        },
        fail: function (err) {
          reject(err);
        }
      });
    });
  },
  // 页面生命周期函数--监听页面加载
  onLoad(options) {
    this.setData({
      a: app.globalcusData.usermessage,
      b: app.globalworkerdata.workermessage
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})